%%work in progress

%Screen('Preference', 'SkipSyncTests', 1); %this is to work in LABS computer

%initial params
screenNum=0;
%screen size (in pixels)
width = 800;
height = 600;
res=[width height];
clrdepth=32;
[wPtr, rect]=Screen('OpenWindow', screenNum, 0, [0 0 res(1) res(2)], clrdepth);

white=WhiteIndex(wPtr);
Screen('FillRect', wPtr, white);

%compute monitor's refresh rate
refresh = Screen('GetFlipInterval', wPtr);
Screen('Flip', wPtr);

%image gallery parameters:
folder = '/home/incc17/incc/incc2011g2/incc2009/Stimuli/morph/morph';
%folder = 'faces/tier1/';
%fileExt = '';
qtyFaces = 50;
aperture = 5;
descending = true;
%descending = mod(round(unifrnd(1,10)),2) == 0;
imgHeight = 435;
imgWidth = 297;
horizMargin = (width - imgWidth*2)/4;
vertMargin = (height - imgHeight)/2;

%experiment variables:
tpp = 0.050; %time per picture
qtyFrames = tpp/refresh; %qty of frames per picture based on monitor's refresh rate.
sequenceSize = 40; %quantity of images to be shown for each sequence (size of trial)
qtyTrials = 1;

%keyCodes:
leftMore = 70; %f
leftLess = 68; %d
rightMore = 74; %j
rightLess = 75; %k

leftMore = 42; %f
leftLess = 41; %d
rightMore = 45; %j
rightLess = 46; %k


%bigDelta: distance inbetween the selected faces.
bigDelta = 5;

% radius of movement for the selected faces. 
sigma = 0;

%midpoint in the line around the selected faces L and R (Uniform)
smallDelta = round(unifrnd(1+sigma,qtyFaces-sigma));  
%sigma adjusts the selectable midpoint 
%in order to avoid out of range situations

%left and right faces in the "hapiness" spectrum
left = smallDelta - bigDelta/2;
right = smallDelta + bigDelta/2;


%preallocate gallery image holders
gallery = cell(1,qtyFaces);

%load images
try
    for i=1:qtyFaces
        file = strcat(folder, num2str(i-1))
        
        gallery{i} = Screen('MakeTexture', wPtr, imread(file));
    end
catch er
    rethrow(er)
end


Screen('DrawText', wPtr, '+', width/2, height/2, [250 0 0]);
vbl = Screen('Flip', wPtr);



for t=1:qtyTrials
    
    for i=1:sequenceSize
        %indexes of images selected from a uniform distribution
        leftImage = round(unifrnd(left - sigma/2, left+sigma/2));
        rightImage = round(unifrnd(right - sigma/2, right+sigma/2));

        %indexes of images selected from a normal distribution
        %leftImage = round(normrnd(left,sigma));
        %rightImage = round(normrnd(right,sigma));

        while (leftImage < 1 || rightImage < 1 || rightImage > qtyFaces || leftImage > qtyFaces)
            %if there's an invalid index, "reshuffle" until achieve a valid
            %value ("busy reshufle" might not be optimal)!!!.
            leftImage = round(normrnd(left,sigma));
            rightImage = round(normrnd(right,sigma));
        end

        Screen('DrawTextures', wPtr, [gallery{leftImage}, gallery{rightImage}], [0 0; 0 0; imgWidth imgWidth; imgHeight imgHeight], [horizMargin width/2+horizMargin; vertMargin vertMargin; horizMargin+imgWidth (width/2+horizMargin + imgWidth); vertMargin+imgHeight vertMargin+imgHeight] ); % [x0 y0 xf yf]
        Screen('DrawText', wPtr, '+', width/2, height/2, [250 0 0]);
        vbl = Screen('Flip', wPtr, vbl + refresh * (qtyFrames - 0.4));
    end
    
    Screen('FillRect',wPtr,white);
    %Screen('DrawText', wPtr, 'Por favor elija cual de los dos est�mulos le pareci� m�s contento.', 100, height/2);
    DrawFormattedText(wPtr, 'Por favor elija cual de los dos \nest�mulos le pareci� m�s contento.', 100, height/2);
    Screen('Flip', wPtr);
    
    [s keyCode ds] = KbStrokeWait();
    keyCode = find(keyCode);
    while (keyCode ~= leftMore && keyCode ~= leftLess && keyCode ~= rightMore && keyCode ~= rightLess)
        Screen('FillRect',wPtr,white);
        DrawFormattedText(wPtr, 'Por favor presione una tecla v�lida. \nF(izq+), D(izq-), J(der+), K(der-) [Sin apretar la tecla "May�s"]', 10, height/2);
        Screen('Flip', wPtr);
        [s keyCode ds] = KbStrokeWait();
        keyCode = find(keyCode);
    end
    Screen('FillRect',wPtr,white);
    DrawFormattedText(wPtr, strcat('Ud apret�: ',KbName(keyCode)), 10, height/2);
    Screen('Flip', wPtr);
    WaitSecs(1);
end

Screen('CloseAll');
ShowCursor;
